<template>
  <div class="home-container">
    <!-- 欢迎横幅 -->
    <section class="hero-section">
      <div class="hero-background">
        <div class="hero-pattern"></div>
      </div>
      <div class="hero-content">
        <div class="hero-text">
          <h1 class="hero-title">
            欢迎来到 <span class="gradient-text">惠学阁</span>
          </h1>
          <p class="hero-subtitle">
            优质学习资源共享平台，助您成功每一步
          </p>
          <p class="hero-description">
            我们提供丰富的学习资料、专业的学习小组，让知识的获取变得更加简单高效。
            加入我们，开启您的学习之旅！
          </p>
          <div class="hero-actions">
            <el-button 
              type="primary" 
              size="large" 
              @click="$router.push('/resources')"
              class="primary-action-btn"
            >
              <el-icon><Search /></el-icon>
              <span>浏览资源</span>
            </el-button>
            <el-button 
              v-if="!isLoggedIn"
              size="large" 
              @click="$router.push('/register')"
              class="secondary-action-btn"
            >
              <el-icon><UserFilled /></el-icon>
              <span>立即注册</span>
            </el-button>
          </div>
        </div>
        <div class="hero-visual">
          <div class="floating-elements">
            <div class="floating-card card-1">
              <el-icon class="card-icon"><Reading /></el-icon>
              <span class="card-text">在线学习</span>
            </div>
            <div class="floating-card card-2">
              <el-icon class="card-icon"><ChatDotRound /></el-icon>
              <span class="card-text">交流讨论</span>
            </div>
            <div class="floating-card card-3">
              <el-icon class="card-icon"><TrendCharts /></el-icon>
              <span class="card-text">学习进度</span>
            </div>
            <div class="floating-card card-4">
              <el-icon class="card-icon"><Medal /></el-icon>
              <span class="card-text">成就系统</span>
            </div>
          </div>
        </div>
      </div>
    </section>

    <!-- 功能特色 -->
    <div class="features-section">
      <div class="section-header">
        <h2 class="section-title">平台特色</h2>
        <p class="section-subtitle">为您提供最优质的学习体验</p>
      </div>
      
      <div class="features-grid">
        <div class="feature-card card">
          <div class="feature-icon">
            <el-icon><FolderOpened /></el-icon>
          </div>
          <h3 class="feature-title">丰富资源</h3>
          <p class="feature-description">
            涵盖各个学科领域的优质学习资源，满足不同学习需求
          </p>
        </div>
        
        <div class="feature-card card">
          <div class="feature-icon">
            <el-icon><UserFilled /></el-icon>
          </div>
          <h3 class="feature-title">学习小组</h3>
          <p class="feature-description">
            加入学习小组，与志同道合的伙伴一起学习成长
          </p>
        </div>
        
        <div class="feature-card card">
          <div class="feature-icon">
            <el-icon><Medal /></el-icon>
          </div>
          <h3 class="feature-title">质量保障</h3>
          <p class="feature-description">
            严格的资源审核机制，确保每一份资料都具有高质量
          </p>
        </div>
        
        <div class="feature-card card">
          <div class="feature-icon">
            <el-icon><Download /></el-icon>
          </div>
          <h3 class="feature-title">便捷下载</h3>
          <p class="feature-description">
            一键下载，支持多种格式，随时随地学习
          </p>
        </div>
      </div>
    </div>

    <!-- 数据统计 -->
    <div class="stats-section">
      <div class="stats-grid">
        <div class="stat-item">
          <div class="stat-number">{{ stats.resources }}</div>
          <div class="stat-label">学习资源</div>
        </div>
        <div class="stat-item">
          <div class="stat-number">{{ stats.users }}</div>
          <div class="stat-label">注册用户</div>
        </div>
        <div class="stat-item">
          <div class="stat-number">{{ stats.downloads }}</div>
          <div class="stat-label">总下载量</div>
        </div>
        <div class="stat-item">
          <div class="stat-number">{{ stats.groups }}</div>
          <div class="stat-label">学习小组</div>
        </div>
      </div>
    </div>

    <!-- 热门资源 -->
    <div class="popular-section" v-if="popularResources.length > 0">
      <div class="section-header">
        <h2 class="section-title">热门资源</h2>
        <p class="section-subtitle">最受欢迎的学习资料</p>
      </div>
      
      <div class="resources-grid">
        <div 
          v-for="(resource, index) in popularResources" 
          :key="resource.resourceId"
          class="resource-card card"
          @click="viewResource(resource)"
        >
          <div class="resource-header">
            <h4 class="resource-title">{{ resource.title }}</h4>
            <div class="resource-tags">
              <el-tag size="small" type="primary">{{ resource.categoryName }}</el-tag>
              <el-tag v-if="index === 0" size="small" type="success">下载最多</el-tag>
              <el-tag v-else-if="index === 1" size="small" type="warning">评分最高</el-tag>
              <el-tag v-else-if="index === 2" size="small" type="info">阅读最多</el-tag>
            </div>
          </div>
          <p class="resource-description">{{ resource.description }}</p>
          <div class="resource-stats">
            <div class="stat-item">
              <el-icon><Download /></el-icon>
              <span>{{ resource.downloadCount }}</span>
            </div>
            <div class="stat-item">
              <el-icon><View /></el-icon>
              <span>{{ resource.viewCount || 0 }}</span>
            </div>
            <div class="stat-item rating-item">
              <span class="rating-value">★{{ (parseFloat(resource.rating) || 0).toFixed(1) }}</span>
            </div>
          </div>
          <div class="resource-footer">
            <div class="resource-meta">
              <span class="uploader">{{ resource.uploaderName }}</span>
              <span class="file-type">{{ resource.fileType?.toUpperCase() }}</span>
            </div>
            <div class="action-buttons">
              <el-button 
                size="small" 
                :type="resource.isFavorited ? 'warning' : 'default'"
                :class="{ 'favorited': resource.isFavorited }"
                @click.stop="favoriteResource(resource)"
              >
                <el-icon><StarFilled /></el-icon>
                {{ resource.isFavorited ? '已收藏' : '收藏' }}
              </el-button>
              <el-button size="small" type="primary" @click.stop="downloadResource(resource)">
                下载
              </el-button>
            </div>
          </div>
        </div>
      </div>
      
      <div class="view-more">
        <el-button @click="$router.push('/resources')">
          查看更多资源
          <el-icon><ArrowRight /></el-icon>
        </el-button>
      </div>
    </div>
  </div>
</template>

<script>
import { ref, computed, onMounted } from 'vue'
import { useStore } from 'vuex'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { resourceApi } from '@/api/resource'
import request from '@/utils/request'
import {
  Search,
  UserFilled,
  Reading,
  ChatDotRound,
  TrendCharts,
  FolderOpened,
  Medal,
  Download,
  ArrowRight,
  View,
  StarFilled
} from '@element-plus/icons-vue'

export default {
  name: 'Home',
  components: {
    Search,
    UserFilled,
    Reading,
    ChatDotRound,
    TrendCharts,
    FolderOpened,
    Medal,
    Download,
    ArrowRight,
    View,
    StarFilled
  },
  setup() {
    const store = useStore()
    const router = useRouter()



    // 响应式数据
    const stats = ref({
      resources: '1,200+',
      users: '5,000+',
      downloads: '50,000+',
      groups: '200+'
    })
    
    const popularResources = ref([])

    // 计算属性
    const isLoggedIn = computed(() => store.getters.getLoginStatus)

    // 获取热门资源
    const fetchPopularResources = async () => {
      try {
        const response = await resourceApi.getPopularResources()
        popularResources.value = response.data
      } catch (error) {
        console.error('获取热门资源失败:', error)
      }
    }

    // 查看资源详情
    const viewResource = (resource) => {
      // 跳转到资源详情页
      router.push(`/resources/${resource.resourceId}`)
    }

    // 下载资源
    const downloadResource = (resource) => {
      if (!isLoggedIn.value) {
        ElMessage.warning('请先登录')
        router.push('/login')
        return
      }
      
      // 这里应该调用下载API
      ElMessage.success(`开始下载：${resource.title}`)
    }

    // 页面加载时获取数据
    onMounted(async () => {
      // 延迟执行以确保DOM完全准备好
      await new Promise(resolve => setTimeout(resolve, 100))
      
      try {
        store.dispatch('setPageTitle', '首页')
        await fetchPopularResources()
      } catch (error) {
        console.error('初始化数据加载失败:', error)
      }
    })

    const favoriteResource = async (resource) => {
      if (!isLoggedIn.value) {
        ElMessage.warning('请先登录')
        return
      }
      
      try {
        if (resource.isFavorited) {
          // 取消收藏
          await request.delete(`/resources/${resource.resourceId}/favorite`)
          resource.isFavorited = false
          ElMessage.success('已取消收藏')
        } else {
          // 添加收藏
          await request.post(`/resources/${resource.resourceId}/favorite`)
          resource.isFavorited = true
          ElMessage.success('收藏成功')
        }
      } catch (error) {
        console.error('收藏操作失败:', error)
        // 如果是已经收藏的错误，则更新状态为已收藏
        if (error.message && error.message.includes('已经收藏')) {
          resource.isFavorited = true
          ElMessage.success('该资源已在您的收藏中')
        } else {
          ElMessage.error('操作失败，请稍后重试')
        }
      }
    }

    return {
      stats,
      popularResources,
      isLoggedIn,
      viewResource,
      downloadResource,
      favoriteResource
    }
  }
}
</script>

<style scoped>
.home-container {
  width: 100%;
  padding: 0;
}

/* 欢迎横幅 */
.hero-section {
  position: relative;
  padding: 80px 0;
  margin-bottom: 80px;
  overflow: hidden;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  width: 100vw;
  margin-left: calc(-50vw + 50%);
}

.hero-background {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: linear-gradient(135deg, rgba(64, 158, 255, 0.5), rgba(102, 126, 234, 0.5));
}

.hero-pattern {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  width: 100%;
  height: 100%;
  background-image: 
    radial-gradient(circle at 25% 25%, rgba(255, 255, 255, 0.1) 0%, transparent 50%),
    radial-gradient(circle at 75% 75%, rgba(255, 255, 255, 0.1) 0%, transparent 50%);
  background-size: 100px 1000px;
  animation: patternMove 20s linear infinite;
}

.hero-content {
  position: relative;
  max-width: 1400px;
  margin: 0 auto;
  padding: 0 24px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 80px;
  z-index: 2;
}

.hero-text {
  flex: 1;
  max-width: 600px;
}

.hero-title {
  font-size: 56px;
  font-weight: 800;
  line-height: 1.1;
  margin-bottom: 24px;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.gradient-text {
  background: linear-gradient(45deg, #fff, #e3f2fd);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.hero-subtitle {
  font-size: 24px;
  margin-bottom: 20px;
  font-weight: 600;
  opacity: 0.95;
}

.hero-description {
  font-size: 18px;
  line-height: 1.7;
  margin-bottom: 40px;
  opacity: 0.9;
  max-width: 500px;
}

.hero-actions {
  display: flex;
  gap: 20px;
  flex-wrap: wrap;
}

.primary-action-btn {
  height: 56px;
  padding: 0 32px;
  font-size: 16px;
  font-weight: 600;
  border-radius: 28px;
  background: white !important;
  color: #409eff !important;
  border: 2px solid white !important;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
  transition: all 0.3s ease;
}

.primary-action-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.15);
  background: #f8f9fa !important;
  color: #409eff !important;
}

.primary-action-btn .el-icon {
  color: #409eff !important;
}

.primary-action-btn span {
  color: #409eff !important;
}

.secondary-action-btn {
  height: 56px;
  padding: 0 32px;
  font-size: 16px;
  font-weight: 600;
  border-radius: 28px;
  background: transparent;
  color: white;
  border: 2px solid rgba(255, 255, 255, 0.3);
  transition: all 0.3s ease;
}

.secondary-action-btn:hover {
  background: rgba(255, 255, 255, 0.1);
  border-color: rgba(255, 255, 255, 0.5);
  transform: translateY(-2px);
}

.hero-visual {
  flex: 1;
  position: relative;
  height: 500px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.floating-elements {
  position: relative;
  width: 100%;
  height: 100%;
}

.floating-card {
  position: absolute;
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
  padding: 20px 24px;
  border-radius: 20px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
  display: flex;
  align-items: center;
  gap: 12px;
  animation: float 6s ease-in-out infinite;
  border: 1px solid rgba(255, 255, 255, 0.2);
  transition: all 0.3s ease;
}

.floating-card:hover {
  transform: translateY(-5px) scale(1.05);
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.15);
}

.card-1 {
  top: 10%;
  right: 15%;
  animation-delay: 0s;
}

.card-2 {
  top: 35%;
  right: 45%;
  animation-delay: 1.5s;
}

.card-3 {
  bottom: 35%;
  right: 20%;
  animation-delay: 3s;
}

.card-4 {
  bottom: 10%;
  right: 50%;
  animation-delay: 4.5s;
}

.card-icon {
  font-size: 28px;
  color: #409eff;
}

.card-text {
  font-size: 14px;
  font-weight: 600;
  color: #303133;
  white-space: nowrap;
}

/* 功能特色 */
.features-section {
  margin-bottom: 100px;
  padding: 0 24px;
  max-width: 1400px;
  margin-left: auto;
  margin-right: auto;
}

.section-header {
  text-align: center;
  margin-bottom: 80px;
}

.section-title {
  font-size: 42px;
  font-weight: 800;
  color: #303133;
  margin-bottom: 16px;
  position: relative;
}

.section-title::after {
  content: '';
  position: absolute;
  bottom: -8px;
  left: 50%;
  transform: translateX(-50%);
  width: 60px;
  height: 4px;
  background: linear-gradient(45deg, #409eff, #667eea);
  border-radius: 2px;
}

.section-subtitle {
  font-size: 18px;
  color: #606266;
  max-width: 600px;
  margin: 0 auto;
  line-height: 1.6;
}

.features-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
  gap: 40px;
}

.feature-card {
  padding: 50px 40px;
  text-align: center;
  transition: all 0.4s ease;
  border-radius: 24px;
  position: relative;
  overflow: hidden;
}

.feature-card::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: linear-gradient(135deg, rgba(64, 158, 255, 0.05), rgba(102, 126, 234, 0.05));
  opacity: 0;
  transition: opacity 0.3s ease;
}

.feature-card:hover::before {
  opacity: 1;
}

.feature-card:hover {
  transform: translateY(-12px);
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.1);
}

.feature-icon {
  width: 100px;
  height: 100px;
  margin: 0 auto 32px;
  background: linear-gradient(45deg, #409eff, #667eea);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 40px;
  color: white;
  position: relative;
  z-index: 2;
  box-shadow: 0 8px 30px rgba(64, 158, 255, 0.3);
}

.feature-title {
  font-size: 24px;
  font-weight: 700;
  color: #303133;
  margin-bottom: 20px;
  position: relative;
  z-index: 2;
}

.feature-description {
  font-size: 16px;
  color: #606266;
  line-height: 1.7;
  position: relative;
  z-index: 2;
}

/* 数据统计 */
.stats-section {
  background: linear-gradient(45deg, #409eff, #667eea);
  padding: 60px 0;
  margin-bottom: 80px;
  width: 100vw;
  margin-left: calc(-50vw + 50%);
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 40px;
  max-width: 1400px;
  margin: 0 auto;
  padding: 0 24px;
}

.stat-item {
  text-align: center;
  color: white;
}

.stat-number {
  font-size: 48px;
  font-weight: bold;
  margin-bottom: 8px;
}

.stat-label {
  font-size: 16px;
  opacity: 0.9;
}

/* 热门资源 */
.popular-section {
  margin-bottom: 60px;
}

.resources-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: 24px;
  margin-bottom: 40px;
}

.resource-card {
  padding: 24px;
  cursor: pointer;
  transition: all 0.3s ease;
}

.resource-card:hover {
  transform: translateY(-4px);
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.15);
}

.resource-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 12px;
}

.resource-title {
  font-size: 16px;
  font-weight: bold;
  color: #303133;
  flex: 1;
  margin-right: 12px;
}

.resource-tags {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.resource-description {
  font-size: 14px;
  color: #606266;
  line-height: 1.5;
  margin-bottom: 16px;
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}

.resource-stats {
  display: flex;
  gap: 16px;
  margin-bottom: 16px;
  padding: 12px 0;
  border-top: 1px solid #f0f0f0;
  border-bottom: 1px solid #f0f0f0;
}

.resource-stats .stat-item {
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: 13px;
  color: #606266;
}

.resource-stats .stat-item .el-icon {
  font-size: 14px;
  color: #909399;
}

.resource-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.action-buttons {
  display: flex;
  gap: 8px;
  align-items: center;
}

.favorited {
  background-color: #ffd04b !important;
  border-color: #ffd04b !important;
  color: #666 !important;
}

.favorited:hover {
  background-color: #ffcd02 !important;
  border-color: #ffcd02 !important;
  color: #666 !important;
}

.rating-item .rating-value {
  color: #ff9900;
  font-weight: 500;
}

.resource-meta {
  display: flex;
  gap: 16px;
  font-size: 12px;
  color: #909399;
}

.uploader {
  font-weight: 500;
}

.file-type {
  background: #f0f2f5;
  padding: 2px 6px;
  border-radius: 4px;
  font-size: 11px;
  font-weight: 600;
  color: #606266;
}

.view-more {
  text-align: center;
}

/* 动画 */
@keyframes float {
  0%, 100% {
    transform: translateY(0px) rotate(0deg);
  }
  33% {
    transform: translateY(-15px) rotate(1deg);
  }
  66% {
    transform: translateY(-25px) rotate(-1deg);
  }
}

@keyframes patternMove {
  0% {
    background-position: 0 0;
  }
  100% {
    background-position: 100px 100px;
  }
}

@keyframes fadeInUp {
  from {
    opacity: 0;
    transform: translateY(30px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.hero-text {
  animation: fadeInUp 1s ease-out;
}

.hero-visual {
  animation: fadeInUp 1s ease-out 0.3s both;
}

/* 响应式设计 */
@media (max-width: 1200px) {
  .hero-content {
    padding: 0 20px;
    gap: 60px;
  }
  
  .features-section {
    padding: 0 20px;
  }
  
  .stats-grid {
    padding: 0 20px;
  }
}

@media (max-width: 768px) {
  .hero-section {
    padding: 60px 0;
  }
  
  .hero-content {
    flex-direction: column;
    text-align: center;
    gap: 50px;
    padding: 0 16px;
  }
  
  .hero-title {
    font-size: 42px;
  }
  
  .hero-subtitle {
    font-size: 20px;
  }
  
  .hero-description {
    font-size: 16px;
    max-width: none;
  }
  
  .hero-actions {
    justify-content: center;
    gap: 16px;
  }
  
  .primary-action-btn,
  .secondary-action-btn {
    height: 48px;
    padding: 0 24px;
    font-size: 15px;
  }
  
  .hero-visual {
    height: 350px;
  }
  
  .floating-card {
    padding: 16px 20px;
  }
  
  .card-icon {
    font-size: 24px;
  }
  
  .card-text {
    font-size: 13px;
  }
  
  .section-title {
    font-size: 32px;
  }
  
  .section-subtitle {
    font-size: 16px;
  }
  
  .features-section {
    padding: 0 16px;
    margin-bottom: 80px;
  }
  
  .section-header {
    margin-bottom: 60px;
  }
  
  .features-grid {
    grid-template-columns: 1fr;
    gap: 30px;
  }
  
  .feature-card {
    padding: 40px 30px;
  }
  
  .feature-icon {
    width: 80px;
    height: 80px;
    font-size: 32px;
    margin-bottom: 24px;
  }
  
  .feature-title {
    font-size: 20px;
  }
  
  .feature-description {
    font-size: 15px;
  }
  
  .stats-grid {
    grid-template-columns: repeat(2, 1fr);
    gap: 30px;
    padding: 0 16px;
  }
  
  .stat-number {
    font-size: 36px;
  }
  
  .resources-grid {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 480px) {
  .hero-title {
    font-size: 32px;
  }
  
  .hero-subtitle {
    font-size: 18px;
  }
  
  .hero-actions {
    flex-direction: column;
    align-items: center;
  }
  
  .primary-action-btn,
  .secondary-action-btn {
    width: 100%;
    max-width: 280px;
  }
  
  .hero-visual {
    height: 280px;
  }
  
  .floating-card {
    padding: 12px 16px;
  }
  
  .card-icon {
    font-size: 20px;
  }
  
  .card-text {
    font-size: 12px;
  }
  
  .section-title {
    font-size: 28px;
  }
  
  .features-grid {
    gap: 24px;
  }
  
  .feature-card {
    padding: 30px 20px;
  }
}
</style> 